//
//  LocationViewController.m
//  哎游
//
//  Created by 林程杰 on 15/1/6.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LocationViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "LocationModel.h"
#import "TravelsDetailModel.h"

@interface LocationViewController ()<MKMapViewDelegate>{
    CLLocationManager *_locationManager;
    MKMapView *_mapView;
}

@end

@implementation LocationViewController
{
    TravelsDetailModel *_model;
}

- (void)dealloc
{
    [_name release];
    [_countryName release];
    [_locationArray release];
    [_lastCountryName release];
    [_lastName release];
    [super dealloc];
}

- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       self.locationArray = [NSMutableArray array];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button setBackgroundImage:[UIImage imageNamed:@"return.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(returnBack) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 50, 45)];
    label.textColor = [UIColor whiteColor];
    label.text = @"地图";
    label.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:20];
    self.navigationItem.titleView = label;
    _mapView.delegate = self;
    
    [label release];
    [button release];
    [self initGUI];
}


#pragma mark - 设置地图

- (void) initGUI
{
    CGRect rect=[UIScreen mainScreen].bounds;
    _mapView=[[MKMapView alloc]initWithFrame:rect];
    [self.view addSubview:_mapView];

    
    //设置代理
    _mapView.delegate=self;
    
        //请求定位服务
//        _locationManager=[[CLLocationManager alloc]init];
//        if(![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse){
//            [_locationManager requestWhenInUseAuthorization];
//        }
//    
//        //用户位置追踪(用户位置追踪用于标记用户当前位置，此时会调用定位服务)
//        _mapView.userTrackingMode=MKUserTrackingModeFollow;
//    
    //设置地图类型
    _mapView.mapType=MKMapTypeStandard;
    
    [self location];
    [_mapView release];
    [_locationManager release];
    
    
}


- (void) location
{
    NSArray *arrLocation = [_locationArray objectAtIndex:1];

    double longitude = [[arrLocation objectAtIndex:1] doubleValue];//经度
    NSLog(@"%f", longitude);
    double latitude = [[arrLocation objectAtIndex:2] doubleValue];//纬度
    NSLog(@"%f", latitude);
    
    NSArray *arrLastLocation = [_locationArray lastObject];
    double lastLongitude = [[arrLastLocation objectAtIndex:1] doubleValue];//终点经度
    NSLog(@"%f", lastLongitude);
    double lastLatitude = [[arrLastLocation objectAtIndex:2] doubleValue];//纬度
    NSLog(@"%f", lastLatitude);
    
    CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(latitude, longitude);
    LocationModel *annotation1=[[LocationModel alloc]init];
    annotation1.title=_name;
    annotation1.subtitle=_countryName;
    annotation1.coordinate=location1;
    [_mapView addAnnotation:annotation1];
    
    CLLocationCoordinate2D location2=CLLocationCoordinate2DMake(lastLatitude, lastLongitude);
    LocationModel *annotation2=[[LocationModel alloc]init];
    annotation2.title=_lastName;
    annotation2.subtitle=_lastCountryName;
    annotation2.coordinate=location2;
    [_mapView addAnnotation:annotation2];
    
    //坐标划线
    MKPlacemark *location11 = [[MKPlacemark alloc] initWithCoordinate:location1
                                                    addressDictionary:nil];

    MKPlacemark *location22   = [[MKPlacemark alloc] initWithCoordinate:location2
                                                      addressDictionary:nil];
    
    MKMapItem *fromItem = [[MKMapItem alloc] initWithPlacemark:location11];
    
    MKMapItem *toItem   = [[MKMapItem alloc] initWithPlacemark:location22];
    
    
    [annotation1 release];
    [annotation2 release];
    [location11 release];
    [location22 release];

    
    [self findDirectionsFrom:fromItem
                          to:toItem];
    
}

#pragma mark - 给两个坐标划线

- (void)findDirectionsFrom:(MKMapItem *)source
                        to:(MKMapItem *)destination
{
//    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    MKDirectionsRequest *request = [MKDirectionsRequest new];
    request.source = source;
    request.destination = destination;
    request.requestsAlternateRoutes = YES;
    
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    
    [directions calculateDirectionsWithCompletionHandler:
     ^(MKDirectionsResponse *response, NSError *error) {
         
         if (error) {
             
             NSLog(@"error:%@", error);
         }
         else {
             
             MKRoute *route = response.routes[0];
             
             [_mapView addOverlay:route.polyline];
         }
     }];
    
    
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    MKPolylineRenderer *renderer = [[[MKPolylineRenderer alloc] initWithPolyline:overlay] autorelease];
    renderer.strokeColor = [UIColor blueColor];
    renderer.lineWidth = 2.0;
    
    return  renderer;
    
}



-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    
    NSLog(@"%@",userLocation);
    //设置地图显示范围(如果不进行区域设置会自动显示区域范围并指定当前用户位置为地图中心点)
        MKCoordinateSpan span=MKCoordinateSpanMake(0.01, 0.01);
        MKCoordinateRegion region=MKCoordinateRegionMake(userLocation.location.coordinate, span);
        [_mapView setRegion:region animated:true];
}

- (void) returnBack
{
    [self.navigationController popViewControllerAnimated:YES];
}




@end
